package com.duan.order.controller;

import com.duan.entity.PageResult;
import com.duan.entity.Result;
import com.duan.entity.StatusCode;
import com.duan.order.pojo.Order;
import com.duan.order.service.OrderService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单表
 */
@RestController
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 查询全部
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Order>> selectAll(){
        return new Result<>("查询成功", orderService.selectAll());
    }


    /**
     * 条件查询
     * @param order
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<Order> > select(@RequestBody Order order){
        return new Result<>("条件查询成功", orderService.selectList(order));
    }

    /**
     * 新增订单
     * @param order
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Order order){
        Integer i = orderService.insert(order);
        if (i == -1){
            return Result.fail("查询不到商品");
        }
        if (i == -2){
            return Result.fail("库存不足");
        }
        if (i == -3){
            return Result.fail("下单失败");
        }
        return new Result<>("新增成功");
    }


    /**
     * 删除订单
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        orderService.delete(ids);
        return new Result<>("删除成功");
    }


    /**
     * 修改订单
     * @param order
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Order order){
        orderService.update(order);
        return new Result<>("修改成功");
    }

    /**
     * 分页查询
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Order> pageList = orderService.pageAll(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 分页条件查询
     * @param order
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody Order order,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Order> pageList = orderService.pageList(order,page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 购物车下单
     * @param
     * @return
     */
    @PostMapping("/addCart")
    public Result addCart(@RequestBody Map map){
        orderService.cartInsert(map);
        return Result.ok("购物车下单成功");
    }

    /**
     * 清空购物车
     * @param
     * @return
     */
    @PostMapping("/clearCart")
    public Result clearCart(@RequestBody Map map){
        orderService.clearCart(map);
        return Result.ok("清空购物车成功");
    }
}
